import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { IccardModelService } from 'src/model/iccard-model/iccard-model.service';
import { RedisService } from 'src/redis/redis.service';
import { hexToStr } from 'src/util';

@Injectable()
export class IccardChargeService {
    private icardChargeOrderMessageKey:string;

    constructor(
        private configService:ConfigService,
        private redis:RedisService,
        private iccardModelService:IccardModelService
    ){
        this.icardChargeOrderMessageKey = this.configService.get<string>('cacheKey.icardChargeOrderMessage')
    
        this.run()
    }

    run() {
        this.redis.redisClient.brpop(this.icardChargeOrderMessageKey, 5, (error, result) => {
            try {
                if(error == null && result !== null) {
                    const { cmd, data} = JSON.parse(result[1])
                    
                } 
            } catch (error) {
                
            }

            this.run()
        })
    }

    response(deviceId:string, packDir:'00'|'01', type:string, str:string) {
        //this.client.publish(this.platformResponseMsgTopic, msgResponse(deviceId, packDir, type, str))
    }

    parse(message:string) {
        const cardId = message.substr(0, 8)
        const action = message.substr(8, 2)
        const cardNo = hexToStr(cardId)
        if(action == '01') { //查询余额
            this.iccardModelService.findOne({cardNo : cardNo}, ['amount', 'rewardAmount']).then(card => {
                if(!card) {

                }
            })
        }
    }
}
